﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Data;
using System.Collections;

public partial class PledgeForm : System.Web.UI.UserControl
{
    int mSelectedClientID = 0;

    Unit mWidth = 820;
    string mTitleWindow = "INTRODUCEREA NOULUI GAJ";
    string appPath = string.Empty;

    public Unit Width
    {
        get { return mWidth; }
        set { mWidth = value; }
    }

    public string TitleWindow
    {
        get { return mTitleWindow; }
        set { mTitleWindow = value; }
    }

    public string PledgeDescription
    {
        get { return selectedGajDescriptionHiddenField.Value + " - " + selectedPledgeTextValue.Value; }
    }

    public string CurrentOperation
    {
        get { return currentOperationIDHiddenField.Value; }
        set { currentOperationIDHiddenField.Value = value; }
    }

    public Hashtable CurrentPledge
    {
        get { return (Hashtable)ViewState[this.ClientID + "PD"]; }
        set { ViewState[this.ClientID + "PD"] = value; }
    }

    public void Show()
    {
        if (currentOperationIDHiddenField.Value.Equals(Constants.Constants.UserOperation.New))
            ClearForm();

        modalPopup.Show();
    }

    public void Hide()
    {
        modalPopup.Hide();
    }
    
    protected void ClearForm()
    {
        selectedGajDescriptionHiddenField.Value = string.Empty;
        selectedPledjeClientIDHiddenField.Value = string.Empty;

        pledgeNameTextBox.Text = string.Empty;
        proprietarPledgeDescriptionTextBox.Text = string.Empty;
        pledgeAddressTextBox.Text = string.Empty;
        pledgeDescription.Text = string.Empty;
        pledgeMarketPrice.Text = string.Empty;
        pledgetAmountTextBox.Text = string.Empty;

        pledgeTypeDDL.SelectedIndex = -1;
    }

    public void BindDataSource()
    {
        clientFilter.BindDataSource();
        DataTable typeList = Utils.ModuleMain().GetClassifierByTypeID((int)Constants.Constants.ClassifierTypes.PledgeType);
        Utils.FillSelector(pledgeTypeDDL, typeList, "Name", "Code");
        ClearForm();        
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        WindowTitleLabel.Text = mTitleWindow;
    }

    protected void pledgeTypeDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        selectedPledgeTextValue.Value = pledgeTypeDDL.SelectedItem.Text;
        proprietarPledgeDescriptionTextBox.Text = selectedGajDescriptionHiddenField.Value + " - " + selectedPledgeTextValue.Value;
        modalPopup.Show();
    }

    protected void clientFilter_ClientSelected(object sender, ClientSelectionControl.FilterWindowEventsArg e)
    {
        int clientID = e.SelectedItem;
        try
        {
            selectedPledjeClientIDHiddenField.Value = clientID.ToString();
            selectedGajDescriptionHiddenField.Value = clientFilter.ClientFirstLastName + " : " + clientFilter.SelectedClientBirthDate.ToString(Constants.Constants.ISODateBackwardDotsFormat);
            proprietarPledgeDescriptionTextBox.Text = selectedGajDescriptionHiddenField.Value + " - " + selectedPledgeTextValue.Value;
            pledgeAddressTextBox.Text = Utils.ModuleCredits().GetClientRealAdress(clientID);
        }
        catch (Exception ex)
        { Utils.InfoText(this.Page, "Erroare in sistem!", ex.Message); }
    }

    protected void clientSelectButton_Clik(object sender, EventArgs e)
    {
        modalPopup.Show();
        clientFilter.Show();
    }

    protected void okButton_Click(object sender, EventArgs e)
    {
        try 
        {
            string denumirea = pledgeNameTextBox.Text.Trim();
            int pledgeType = 0;
            int.TryParse(pledgeTypeDDL.SelectedValue, out pledgeType);

            int clientID = clientFilter.SelectedClientID;
            string address = pledgeAddressTextBox.Text.Trim();
            string description = pledgeDescription.Text.Trim();

            int pretpiata = 0;
            int.TryParse(pledgeMarketPrice.Text, out pretpiata);

            int sumagajata = 0;
            int.TryParse(pledgetAmountTextBox.Text, out sumagajata);

            Hashtable resultPledge = new Hashtable();

            if (currentOperationIDHiddenField.Value.Equals((string)Constants.Constants.UserOperation.New))
            {
                resultPledge.Add("LoanID", 0);
                resultPledge.Add("pledgeID", 0);
                resultPledge.Add("denumirea", denumirea);
                resultPledge.Add("pledgeType", pledgeType);
                resultPledge.Add("proprietar", clientID);
                resultPledge.Add("address", address);
                resultPledge.Add("description", description);
                resultPledge.Add("pretpiata", pretpiata);
                resultPledge.Add("sumagajata", sumagajata);
                resultPledge.Add("ShortDescription", selectedGajDescriptionHiddenField.Value + " - " + selectedPledgeTextValue.Value);
            }
            else
            {
                resultPledge = (Hashtable)ViewState[this.ClientID + "PD"];
                resultPledge["denumirea"] = denumirea;
                resultPledge["pledgeType"] = pledgeType;
                resultPledge["proprietar"] = clientID;
                resultPledge["address"] = address;
                resultPledge["description"] = description;
                resultPledge["pretpiata"] = pretpiata;
                resultPledge["sumagajata"] = sumagajata;
                resultPledge["ShortDescription"] = selectedGajDescriptionHiddenField.Value + " - " + selectedPledgeTextValue.Value;
            }

            FilterWindowEventsArg args = new FilterWindowEventsArg(resultPledge);
            OnClientSelected(this, args);
            ViewState[this.ClientID + "PD"] = resultPledge;
            modalPopup.Hide();
        }
        catch (Exception ex)
        { Utils.InfoText(this.Page, "Errore in sistem", ex.Message); }        
    }

    public class FilterWindowEventsArg : EventArgs
    {
        private Hashtable selectedItem;
        public Hashtable SelectedItem
        {
            get { return selectedItem; }
        }

        public FilterWindowEventsArg(Hashtable item)
        {
            selectedItem = item;
        }
    }

    public delegate void CustomItemEventHandler(object sender, FilterWindowEventsArg e);

    public event CustomItemEventHandler OnClientSelected;

    

}